package com.egao.api.controller.nont;


import com.egao.api.base.BaseApiController;
import com.egao.common.core.constants.CacheConst;
import com.egao.common.core.utils.messages.PhoneUtils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.service.OperRecordService;
import com.egao.common.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * api登录相关接口
 * Created by wangfan on 2019-05-29 下午 3:43.
 * http://localhost/swagger-ui.html
 */
@RequestMapping("/api/nt/message")
@RestController
@Api(value = "短信API",tags = {"短信API"})
public class ApiMessagesNtController extends BaseApiController {

    @Autowired
    private UserService userService;
    @Autowired
    private OperRecordService operRecordService;

    /**
     * 发送验证码
     */
    @PostMapping("/messageVerify")
    @ApiOperation(value = "获取验证码",notes = "phoneNumber 手机号码",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneNumber",value = "手机",paramType = "body",dataType = "String",required = true),
    })
    @ResponseBody
    public JsonResult messageVerify(String phoneNumber, HttpServletRequest request) throws Exception {
        String random = (int)((Math.random()*9+1)*1000) + "";
        PhoneUtils.sendMsg(phoneNumber, "596942", new String[]{random});
        return JsonResult.ok("验证码").put("data", random);
    }

    /**
     * 用户重置密码 获取验证码
     */
    @PostMapping("/userMessageVerify")
    @ApiOperation(value = "用户重置密码 获取验证码",notes = "" +
            "phoneNumber: 手机号码 必填\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult userMessageVerify(String phoneNumber, HttpServletRequest request) throws Exception {
        String random = (int)((Math.random()*9+1)*1000) + "";
        PhoneUtils.sendMsg(phoneNumber, "596942", new String[]{random});

        // 1. 创建缓存管理器d:\\logFile\\sszg_chf\\
//        CacheManager cacheManager = CacheManager.create("./src/main/resources/ehcache/ehcache-shiro.xml");
        CacheManager cacheManager = CacheManager.create("d:/ehcache/ehcache-shiro.xml");
        // 2. 获取缓存对象
        Cache cache = cacheManager.getCache(CacheConst.PHONEMESSAGEVERIFY);
        // 3. 创建元素
        Element element = new Element(phoneNumber, random);
        // 4. 将元素添加到缓存
        cache.put(element);

//        HttpSession session = request.getSession(true);
//        session.setAttribute(phoneNumber, random);
        return JsonResult.ok("验证码").put("data", random);
    }




}
